package pl.edu.pb.wi.cookbook.controller;

import java.util.Arrays;
import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import pl.edu.pb.wi.cookbook.entity.Recipe;
import pl.edu.pb.wi.cookbook.form.NewRecipeForm;
import pl.edu.pb.wi.cookbook.service.RecipeService;

@Controller
@RequestMapping("/admin/recipe")
public class AdminRecipeController {
    private static final Logger log = Logger
            .getLogger(AdminCategoryController.class.getName());

    private RecipeService recipeService;

    @Autowired
    public AdminRecipeController(RecipeService recipeService) {
        this.recipeService = recipeService;
    }

    @RequestMapping("delete/{id}")
    public String delete(@PathVariable("id")
    Long id, Model model) {
        String returnPath = "forward:/recipe/list";

        try {
            Recipe rec = recipeService.getRecipe(id);
            if (rec != null) {
                recipeService.removeRecipe(rec);

                model.addAttribute("successMsgs", Arrays
                        .asList(new String[] { String.format(
                                "Przepis '%s' usunięto pomyślnie",
                                rec.getName()) }));
            } else {
                model.addAttribute("errorMsgs", Arrays
                        .asList(new String[] { "Nieprawidłowy przepis." }));
            }
        } catch (Exception e) {
            log.info(String.format(
                    "Problem with deleteform form. Exception: %s",
                    e.getMessage()));

            model.addAttribute(
                    "errorMsgs",
                    Arrays.asList(new String[] { "Wystąpił błąd. Spróbuj ponownie." }));

            returnPath = "message";
        }

        return returnPath;
    }

    @RequestMapping(value = "edit/{id}", method = RequestMethod.GET)
    public String edit(@PathVariable("id")
    long id, Model model) {
        String returnPath = "message";

        try {
            Recipe rec = recipeService.getRecipe(id);

            if (rec != null) {
                model.addAttribute("id", id);
                model.addAttribute("form", converRecipeToRecipeForm(rec));

                returnPath = "user/recipe/edit";

            } else {
                model.addAttribute("errorMsgs", Arrays
                        .asList(new String[] { "Nieprawidłowy przepis." }));
            }
        } catch (Exception e) {
            log.info(String.format("Problem with editform form. Exception: %s",
                    e.getMessage()));

            model.addAttribute(
                    "errorMsgs",
                    Arrays.asList(new String[] { "Wystąpił błąd. Spróbuj ponownie." }));
        }

        return returnPath;
    }

    private NewRecipeForm converRecipeToRecipeForm(Recipe rec) {
        NewRecipeForm form = new NewRecipeForm();
        form.setCategory(rec.getCategory().getId());
        form.setImageUrl(rec.getImageUrl());
        form.setIngredients(rec.getIngredients());
        form.setIsPrivate(rec.isPrivate());
        form.setName(rec.getName());
        form.setPrepareTime(rec.getPrepareTime());
        form.setServings(rec.getServings());
        form.setShortDescription(rec.getShortDescription());
        form.setDescription(rec.getDescription());

        return form;
    }
}
